package com.zjsru.binaryeasy;

/**
 * @Author: likew
 * @Date: 2022/8/6
 * 寻找比目标字母大的最小字母
 *
 * 输入: letters = ["c", "f", "j"]，target = "a"
 * 输出: "c"
 *
 * 输入: letters = ["c","f","j"], target = "c"
 * 输出: "f"
 *
 * 输入: letters = ["c","f","j"], target = "d"
 * 输出: "f"
 */
public class NextGreatestLetter {
    public char nextGreatestLetter(char[] letters, char target) {
        int r = letters.length - 1, l = 0;
        while (l < r){
            int mid = (r - l)/2 + l;
            if(letters[mid] > target){
                r = mid;
            }else{
                l = mid + 1;
            }
        }
        return letters[r] > target ? letters[r] : letters[0];
    }
    
    public static void main(String[] args) {
        NextGreatestLetter nextGreatestLetter = new NextGreatestLetter();
        char[] letters = new char[]{'c', 'f', 'j'};
        char target = 'd';
        System.out.println(nextGreatestLetter.nextGreatestLetter(letters, target));
    }
}
